#include "Ball.h"
#include "Home.h"
#include "Mallet.h"
#define convForceVitesse 0.1

Ball::Ball(GameRectangle & arene):CircleMovable(Vector2D((arene.TR.X+arene.BL.X)*0.5,(arene.TR.Y+arene.BL.Y)*0.5),15,arene),_vitesse(0),_resistance(0.01)
{
	_couleur = "Red";
}

Ball::~Ball(void)
{
}
void Ball::InitEffect()
{
}
void Ball::AddVitesse(double v)
{
	_vitesse+=v;
	if(_vitesse<0)
		_vitesse=0;
	if(_vitesse>10)
		_vitesse = 10;
}
void Ball::InitBall()
{
	_vitesse = 0;
	_direction = Vector2D();
	_centre = Vector2D((_arene.TR.X+_arene.BL.X)*0.5,(_arene.TR.Y+_arene.BL.Y)*0.5);
	_invalidEnglobant = true;
}
int Ball::Collision(DrawableObject *ob)
{
	if(ob==this)
		return 0;
	return CircleMovable::Collision(ob);
}

void Ball::SetMoveParam(Vector2D & direction, double force, double resistance)
{
	_direction = direction;
	if(force>-1)
	{
		_vitesse = 0;
		AddVitesse(force*convForceVitesse);
	}
	if(resistance>-1)
		_resistance = resistance;
}
void Ball::Anime(vector<DrawableObject*> & liste)
{
	if(_vitesse<THETA_VITESSE)
		return;

	for(int i=0; i<liste.size();i++)
	{
		int coll = Collision(liste[i]);
		if(coll)
		{
			CircleMovable * obj = dynamic_cast<CircleMovable *>(liste[i]);
			if(obj!=NULL)
				CalculerDirection(obj);
			else
			{
				Home * obj = dynamic_cast<Home *>(liste[i]);
				if(obj!=NULL && obj->Collision(this))
				{
					InitBall();
				}
				else CalculerDirection(coll);
			}
		}
	}

	Vector2D aux = _direction;
	AddVitesse(-_resistance);
	_direction*=_vitesse;
	Move();
	_direction = aux;
}